package cn.ray_land.dlprint.print;

import android.content.DialogInterface;
import android.media.MediaRouter;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import cn.ray_land.dlprint.R;
import cn.ray_land.dlprint.data.SliceData;

/**
 * Created by gw on 2016/8/3.
 */

public class PrintActivity extends AppCompatActivity implements PrintContract.View{

    private MediaRouter mediaRouter;
    private OverShadowPresentation mPresentation;
    private ImageView imageView;
    private TextView progressText;
    private Button stopButton;
    private PrintContract.Presenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        init();
        mPresenter.startPrint();
    }

    private void init() {
        mPresenter = new PrintPresenter((SliceData) getIntent().getSerializableExtra("sliceData"), this);
        mediaRouter = (MediaRouter) getSystemService(MEDIA_ROUTER_SERVICE);

        imageView = (ImageView) findViewById(R.id.iv_current_layer_image);
        progressText = (TextView) findViewById(R.id.tv_progress_text);
        stopButton = (Button) findViewById(R.id.btn_stop_print);
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void showProgressText(int current, int total) {
        progressText.setText(current+" / "+total);
    }

    @Override
    public void showImage(Uri uri) {
        imageView.setImageURI(uri);
        if(mPresentation!=null){
            mPresentation.setPresentationImage(uri);
        }
    }

    private final MediaRouter.SimpleCallback mMediaRouterCallback =
            new MediaRouter.SimpleCallback() {

                @Override
                public void onRouteSelected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
                    updatePresentation();
                }

                @Override
                public void onRouteUnselected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
                    updatePresentation();

                }

                @Override
                public void onRoutePresentationDisplayChanged(MediaRouter router, MediaRouter.RouteInfo info) {
                    updatePresentation();
                }

            };

    private final DialogInterface.OnDismissListener mOnDismissListener =
            new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dialog == mPresentation) {
                        mPresentation = null;
                    }
                }
            };

    private void updatePresentation() {
        MediaRouter.RouteInfo selectedRoute = mediaRouter.getSelectedRoute(
                MediaRouter.ROUTE_TYPE_LIVE_VIDEO);

        Display selectedDisplay = null;
        if (selectedRoute != null) {
            selectedDisplay = selectedRoute.getPresentationDisplay();
        }

        if (mPresentation != null && mPresentation.getDisplay() != selectedDisplay) {
            mPresentation.dismiss();
            mPresentation = null;
        }

        if (mPresentation == null && selectedDisplay != null) {

            // Initialise a new Presentation for the Display
            mPresentation = new OverShadowPresentation(this, selectedDisplay);
            mPresentation.setOnDismissListener(mOnDismissListener);

            try {
                mPresentation.show();
            } catch (WindowManager.InvalidDisplayException ex) {
                mPresentation = null;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouterCallback);
        updatePresentation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mediaRouter.removeCallback(mMediaRouterCallback);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mPresentation != null) {
            mPresentation.dismiss();
            mPresentation = null;
        }
    }

    @Override
    protected void onDestroy() {
        mPresenter.stopPrint();
        super.onDestroy();
    }
}
